﻿using System;
using System.Collections.Generic;
using System.Text;
using System.Xml;

namespace HtmlHelper
{
    public class XhtmlDocument:XmlDocument
    {
        public static List<string> singleTags = new List<string>(new string[] { "area", "base", "basefont", "br", "col", "frame", "hr", "img", "input", "link", "meta", "param" });

        public XhtmlDocument(bool createBasicElememts): base()
        {
            this.XmlResolver = null;
            if (createBasicElememts)
            {
                this.LoadXml(@"<!DOCTYPE HTML PUBLIC ""-//W3C//DTD HTML 4.0 Transitional//EN"" ""http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd"">
<html xmlns=""http://www.w3.org/1999/xhtml"">
	<head>
		<title></title>
	</head>
	<body>
	
	</body>
</html>");
            }
        }


        private Uri absoluteUrl = null;
        public Uri AbsoluteUrl
        {
            get { return this.absoluteUrl; }
            set { this.absoluteUrl = value; }
        }



        public XhtmlDocument(string html)
        {
            XmlNode CurrentNode = this;
            string[] elements = XhtmlRegex.SpliteHtmlElement(html);
            for (int i = 0; i < elements.Length; i++)
            {
                if (elements[i].Trim() != "")
                {
                    if (elements[i].StartsWith("</"))
                    {
                        string name = XhtmlRegex.getTagName(elements[i]).ToLower();
                        XmlNode mathedPairNode = CurrentNode;
                        while (mathedPairNode != null && mathedPairNode.LocalName.ToLower() != name)
                        {
                            mathedPairNode = mathedPairNode.ParentNode;
                        }
                        if (mathedPairNode != null)
                        {
                            CurrentNode = mathedPairNode.ParentNode;
                        }
                    }
                    else if (elements[i].StartsWith("<!--"))
                    {
                        XmlDocumentFragment docFrag = this.CreateDocumentFragment();
                        docFrag.InnerXml = elements[i];
                        CurrentNode.AppendChild(docFrag);
                    }
                    else if (elements[i].ToLower().StartsWith("<!doctype"))
                    {
                        XmlDocumentFragment docFrag = this.CreateDocumentFragment();
                        docFrag.InnerXml = elements[i];
                        this.AppendChild(docFrag);
                    }
                    else if (elements[i].ToLower().StartsWith("<script"))
                    {
                        //CurrentNode.htmlType = HtmlElementType.Script;
                        //CurrentNode.elementName = "script";
                        //CurrentNode.attributes = new HtmlAttributeCollection(elements[i]);
                        //this.attributes.HtmlChanged += new EventHandler(element_HtmlChanged);
                        //CurrentNode.text = HtmlParser.geScriptText(elements[i]);
                        //break;
                    }
                    else if (elements[i].StartsWith("<"))
                    {
                        //CurrentNode.htmlType = HtmlElementType.Tag;
                        //CurrentNode.elementName = HtmlParser.getTagName(elements[i]).ToLower();
                        //CurrentNode.attributes = new HtmlAttributeCollection(elements[i]);
                        //this.attributes.HtmlChanged += new EventHandler(element_HtmlChanged);
                        //CurrentNode.childs.Clear();
                    }
                    else
                    {
                        //CurrentNode.htmlType = HtmlElementType.Text;
                        //CurrentNode.elementName = "";
                        //CurrentNode.attributes.Clear();
                        //CurrentNode.childs.Clear();
                        //CurrentNode.text = value;
                        //break;
                    }
                }
                //if (HtmlElement.singleTags.Contains(CurrentNode.elementName) || CurrentNode.htmlType != HtmlElementType.Tag)
                //{
                //    CurrentNode = CurrentNode.Parent;
                //}
                
                
                //else
                //{
                //    //HtmlElement element = new HtmlElement(elements[i]);
                //    //element.setParent(CurrentNode);
                //    //CurrentNode = element;
                //}
            }
        }
        

        public void save(string path, string FolderPath)
        {

        }

        public string compress(string html)
        {
            return "";
        }

        private bool f_isDirty = true;
        public bool IsDirty
        {
            get
            {
                return f_isDirty;
            }
            set
            {
                f_isDirty = value;
            }
        }

    }
}
